/** @jsxImportSource @emotion/react */
import * as React from "react";
import {InspectRecordLayout, InternalItemProps, OriginalViewProps, useItemInputControl,} from "../../common/base";
import {assertNamesUnique, createItem,} from "../../common/eHelper";
import {useRecordListSubr} from "../../hook/useRecordListSub";
import {DeviceSurvey} from "../../elevator/editor";
import {useMedia} from "use-media";
import {Button, Text, TextArea, useToast} from "customize-easy-ui-component";
import {EditStorageContext} from "../../StorageContext";
import {render设备类别} from "../../common/render";

const 制造许级选=['A1','A2','A3', ];
//有关联台账的：只能从台账 去改的。 被snapshot优先覆盖掉。
export const config证书概要 = [
  [['制造单位', '_$制造单位']],
  [['制造许可级别', {n:'制造许级',t:'l',l:制造许级选}], ['制造许可证编号', '制许可号']],
  [['设备类别', '_$设备类别',render设备类别], ['产品名称','_$设备名称']],
  [['产品编号', '_$出厂编号'], ['设备代码', '_$设备代码']],
  [['设计单位', '_$设计单位']],
  [['设计许可证编号', '设许证号'], ['产品图号', '_$设计图号']],
  [['设计日期', '_$设计日期'], ['制造日期', '_$制造日期' ]],
];

export const EntranceSetup =
    React.forwardRef((
        {children, show, alone = true, repId, verId, rep}: InternalItemProps, ref
    ) => {
      const atPrint = useMedia('print');
      const toast = useToast();
      const doCheckNames = React.useCallback((verId: string) => {
        const result = assertNamesUnique([{value: rep?.tzFields},
          {value: config证书概要, type:'esnt'},
          ]);
        toast({title: "完成！", subtitle: result ? "没发现冲突" : "测试开关没开", intent: "success"});
      }, [rep,toast]);
      const {storage, setStorage} =React.useContext(EditStorageContext) as any;
      const getInpFilter = React.useCallback((par: any) => {
        const {_tblFixed, } =par||{};
        return {_tblFixed, };
      }, []);
      const {inp, setInp} = useItemInputControl({ref});
      if (atPrint) return null;
      else return (
          <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                               alone={alone} label={'初始化本报告，默认值配置等'}>
            {process.env.REACT_APP_TEST==='true' && <><div>
              <Text variant="h5">构建开发模板时的工具：校验模板的存储name冲突；</Text>
              <Button intent='primary' onPress={() => doCheckNames(verId!)}>校验模板name唯一性</Button>
              <Text variant="h5">设置待测试表格的各列宽度：</Text>
              <TextArea value={storage?._tblFixed || ''} rows={2}
                        onChange={e => setStorage({ ...storage, _tblFixed: e.currentTarget.value || undefined}) }/>
            </div>
            </>
            }
            <hr/>
          </InspectRecordLayout>
  );
});


const recordPrintList =[
  createItem('Entrance', <EntranceSetup/>),
  createItem('CertificateSummary', <DeviceSurvey config={config证书概要} label={'特种设备制造监督检验证书'}/>),
];

export const OriginalView=
  React.forwardRef((
    { action: modAction,  verId, repId='', rep}
    :OriginalViewProps, ref
  ) => {
    const {list}=useRecordListSubr(ref,rep,recordPrintList,modAction,verId);
    return <React.Fragment>
      {list}
    </React.Fragment>;
  } );
